package com.guet.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author MicahYin
 * @date 2020/8/4 21:29
 */
//实现自定义拦截器，当用户未登录时候，将其拦截
public class LoginHandlerInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");
        if(user==null){
            request.setAttribute("msg","没有权限,请先登录");
            //此处转跳的时候，如果只配置index.html会有报错，要和Config里面的配置一样，即/index.html才不会出现错误信息
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }
        return true;
    }
}
